home *** CD-ROM | disk | FTP | other *** search
- //____________________________________________________________
- // CopyBitsB&W.c
- //
- // Copyright © Dan Parks Sydow, 1995
- // From the book:
- // "Graphics and Sound Programming Techniques for the Mac",
- // M&T Books, 1995
-
-
- //____________________________________________________________
-
- #define rDisplayWindow 128
- #define rBackPicture 128
- #define rForePicture 129
-
-
- //____________________________________________________________
-
- void InitializeToolbox( void );
- GrafPtr MakeNewBitMapAndSetPort( Rect * );
-
-
- //____________________________________________________________
-
- void main( void )
- {
- WindowPtr theWindow;
- GrafPtr theBackGrafPtr;
- GrafPtr theForeGrafPtr;
- GrafPtr theMergeGrafPtr;
- PicHandle theBackPicture;
- PicHandle theForePicture;
- Rect theRect;
- short theWidth;
- short theHeight;
- short left = 135;
- short top = 50;
- short count = 0;
-
- InitializeToolbox();
-
- HideCursor();
-
- theBackPicture = GetPicture( rBackPicture );
- if ( theBackPicture == nil )
- ExitToShell();
-
- theRect = (**theBackPicture).picFrame;
- OffsetRect( &theRect, - theRect.left, - theRect.top );
-
- theWidth = theRect.right - theRect.left;
- theHeight = theRect.bottom - theRect.top;
-
- theWindow = GetNewWindow( rDisplayWindow, nil, (WindowPtr)-1L );
- SizeWindow( theWindow, theWidth, theHeight, true );
- ShowWindow( theWindow );
-
- theBackGrafPtr = MakeNewBitMapAndSetPort( &theRect ); // 1st bitmap
- DrawPicture( theBackPicture, &theRect );
-
- theMergeGrafPtr = MakeNewBitMapAndSetPort( &theRect ); // 2nd bitmap
-
- theForePicture = GetPicture( rForePicture );
- if ( theBackPicture == nil )
- ExitToShell();
-
- theRect = (**theForePicture).picFrame;
-
- theWidth = theRect.right - theRect.left;
- theHeight = theRect.bottom - theRect.top;
- SetRect( &theRect, left, top, left + theWidth, top + theHeight );
-
- theForeGrafPtr = MakeNewBitMapAndSetPort( &theRect ); // 3rd bitmap
- DrawPicture( theForePicture, &theRect );
-
- while ( !Button() )
- {
- ++count;
-
- if ( count <= 50 )
- ++top;
- else if ( ( count > 50 ) && ( count <= 100 ) )
- --top;
- else
- {
- count = 1;
- ++top;
- }
-
- CopyBits( &(theBackGrafPtr->portBits), &(theMergeGrafPtr->portBits),
- &(theBackGrafPtr->portBits.bounds), &(theMergeGrafPtr->portBits.bounds),
- srcCopy, nil );
-
- theRect = theForeGrafPtr->portBits.bounds;
-
- SetRect( &theRect, left, top, left + theWidth, top + theHeight );
-
- CopyBits( &(theForeGrafPtr->portBits), &(theMergeGrafPtr->portBits),
- &(theForeGrafPtr->portBits.bounds), &theRect,
- srcOr, nil );
-
- CopyBits( &(theMergeGrafPtr->portBits), &(theWindow->portBits),
- &(theMergeGrafPtr->portBits.bounds), &(theWindow->portRect),
- srcCopy, nil );
- }
- }
-
-
- //____________________________________________________________
-
- GrafPtr MakeNewBitMapAndSetPort( Rect *theRectPtr )
- {
- BitMap *theBitMapPtr;
- short theImageHeight;
- short theImageByteSize;
- GrafPtr theGrafPtr;
-
- theBitMapPtr = (BitMap *)NewPtr( sizeof( BitMap ) );
- if ( theBitMapPtr == nil )
- ExitToShell();
-
- theBitMapPtr->bounds = *theRectPtr;
-
- theBitMapPtr->rowBytes = (theRectPtr->right - theRectPtr->left + 7) / 8;
- if ( ( theBitMapPtr->rowBytes % 2 ) != 0 )
- theBitMapPtr->rowBytes++;
-
- theImageHeight = theRectPtr->bottom - theRectPtr->top;
- theImageByteSize = theBitMapPtr->rowBytes * theImageHeight;
- theBitMapPtr->baseAddr = NewPtr( theImageByteSize );
- if ( theBitMapPtr->baseAddr == nil )
- ExitToShell();
-
- theGrafPtr = (GrafPtr)NewPtr( sizeof( GrafPort ) );
- if ( theGrafPtr == nil )
- ExitToShell();
-
- OpenPort( theGrafPtr );
- SetPortBits( theBitMapPtr );
-
- return( theGrafPtr );
- }
-
-
- //____________________________________________________________
-
- void InitializeToolbox( void )
- {
- InitGraf( &qd.thePort );
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs( 0L );
- FlushEvents( everyEvent, 0 );
- InitCursor();
- }